package com.hzcx.app.simplechat.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.friend.FriendSearchActivity;
import com.hzcx.app.simplechat.ui.home.bean.AddFriendSearchBean;
import com.hzcx.app.simplechat.ui.home.contract.AddFriendSearchContract;
import com.hzcx.app.simplechat.ui.home.presenter.AddFriendSearchPresenter;
import com.hzcx.app.simplechat.ui.login.AddUserInfoActivity;
import com.hzcx.app.simplechat.ui.user.UserInfoActivity;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加好友-搜索
 */
public class AddFriendSearchActivity extends BaseActivity<AddFriendSearchPresenter> implements AddFriendSearchContract.View {

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.tv_search_content)
    TextView tvSearchContent;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;

    @Override
    public int getLayout() {
        return R.layout.activity_add_friend_search;
    }

    @Override
    public void initPresenter() {
        mPresenter = new AddFriendSearchPresenter();
    }

    @Override
    public void initView() {

        // 弹出软键盘
        AppHelper.showSoftKeyboard(etSearch, this);

        etSearch.setHint("搜索手机号/" + getResources().getString(R.string.app_name) + "号");
        tvTips.setText("搜索手机号/" + getResources().getString(R.string.app_name) + "号");

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etSearch.getText().toString().length() > 0) {
                    ivClear.setVisibility(View.VISIBLE);
                    llSearch.setVisibility(View.VISIBLE);
                    tvTips.setVisibility(View.GONE);
                    tvSearchContent.setText(etSearch.getText().toString());
                } else {
                    ivClear.setVisibility(View.GONE);
                    llSearch.setVisibility(View.GONE);
                    tvTips.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etSearch.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                if (EmptyUtils.isEmpty(etSearch.getText().toString())) {
                    return false;
                }
                mPresenter.searchFriend(AddFriendSearchActivity.this, etSearch.getText().toString());
                return true;
            }
            return false;
        });
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.iv_clear, R.id.ll_search})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_clear:
                etSearch.setText("");
                break;

            case R.id.ll_search:
                mPresenter.searchFriend(this, etSearch.getText().toString());
                break;
        }
    }

    /**
     * 搜索成功
     *
     * @param data
     */
    @Override
    public void searchResult(AddFriendSearchBean data) {
        if (data == null) {
            showTips(null, "未搜索到该用户", null);
            return;
        }
        if (data.getMember_id() == UserInfoUtil.getUserId()) {
            showTips(null, "不能添加自己为好友", null);
            return;
        }
        if (data.getIs_buddy() == 1) {
            if (data.getIs_del() == 1) {
                startActivity(new Intent(this, AddFriendActivity.class)
                        .putExtra(AddFriendActivity.INTENT_USER_ID, data.getMember_id()));
            } else {
                startActivity(new Intent(this, UserInfoActivity.class)
                        .putExtra(UserInfoActivity.INTENT_USER_ID, data.getMember_id()));
            }
        } else {
            startActivity(new Intent(this, AddFriendActivity.class)
                    .putExtra(AddFriendActivity.INTENT_USER_ID, data.getMember_id()));
        }
    }
}
